<?php
class Crawl_sina_weibo_detial_library extends Crawl_keyword_library {
	private $_header;
	private $_userAgent;
	private $_url;
	private $_cookie;
	private $_parameters = '';
	public $_htmlsearch_config;
	private $temp_url;
	
	/**
	 * 定义 CURL 消息头、用户端信息、抓取地址
	 * 定义 新浪网页抓取选项
	 */
	function __construct() {
		parent::__construct ();
		$openplatforms = $this->_ci->config->item ( 'openplatforms' );
		$this->_htmlsearch_config = $openplatforms ['openplatforms'] ['htmlsearch'] ['sina_phone'];
		$this->_null_tab = $this->_htmlsearch_config ['null'];
		
		$this->_header = $this->_htmlsearch_config ['header'];
		$this->_userAgent = $this->_htmlsearch_config ['user_agent'];
		$this->_url = $this->_htmlsearch_config ['url'];
		
		$this->write_log_stary_tag ();
	}
	public function crawl($retry = FALSE) {
		$crawl_result = $this->try_crawl ();
		// $filename = "/home/www/crawl/application/logs/weibo.txt";
		// file_put_contents($filename, $crawl_result);
		
		$crawl_result = $this->get_content_file_li( $this->_parameters ['url'] );

		if (is_array ( $crawl_result ) && array_key_exists ( $this->_error_string, $crawl_result )) {
			return $crawl_result;
		} else if (is_array ( $crawl_result ) && array_key_exists ( 'data', $crawl_result ) && is_null ( $crawl_result ['data'] )) {
			return $crawl_result;
		} else {
			return $this->make_data ( $crawl_result );
		}
	}
	private function make_data($result = NULL) {
		$this->_ci->load->model ( 'split/sina_detial_model' );
		$split_data = $this->_ci->sina_detial_model->get_sina_weibo_detial ( $result );
		
		if ($split_data == FALSE) {
			$error = $this->_ci->error_code_library->getError ( 'make_data', 'data', 'match_split_data' );
			log_message ( self::LOG_LEVEL_WARING, json_encode ( $error ), FALSE, $this->_log_dir );
			return $error;
		}

        $user_content = $this->get_content_file_li($split_data['user']['profile_url']);
        $user_data = $this->_ci->sina_detial_model->get_sina_weibo_user_info ( $user_content );

        if(!empty($user_data)) {
            $split_data['user']['friends_count'] = $user_data['friends_count'];
            $split_data['user']['followers_count'] = $user_data['followers_count'];
        }

		$split_data ['url'] = $this->temp_url;
		return $split_data;
	}
	
	/**
	 * 尝试抓取
	 *
	 * @param bool $retry
	 *        	是否使用代理和重试功能
	 * @return array|string 错误时返回error数组 正确时返回抓取到的字符穿
	 */
	private function try_crawl($retry = FALSE) {
		// 临时数组
		$temp_array = array ();
		// 定义抓取参数
		$parameters = $this->make_parameters ();
		
		if (is_array ( $parameters ) && array_key_exists ( $this->_error_string, $parameters )) {
			log_message ( $this->_error_string, 'Crawl From Sina Wap Make CURL Parameters Error', FALSE, $this->_log_dir );
			return $parameters;
		}
		if ($retry == FALSE) {
			// 不使用代理、不重试直接抓取
			log_message ( self::LOG_LEVEL_NOTICE, 'Will Crawl From Sina Wap Un Use Retry And Un Use Proxy With Parameters: ' . json_encode ( $this->_parameters ), FALSE, $this->_log_dir );
			return self::get_content ( $this->_parameters );
		} else {
			$crawl_use_proxy_ip_result = $this->crawl_use_proxy_ip ( $this->_parameters, self::PRROXY_IP, $this->_crawl_from ['sina_phone'], TRUE );
			if (is_array ( $crawl_use_proxy_ip_result )) {
				if (! array_key_exists ( $this->_error_string, $crawl_use_proxy_ip_result )) {
					$temp_array = $crawl_use_proxy_ip_result;
				}
				
				$re_crawl_result = $this->crawl_use_proxy_ip ( $this->_parameters, self::MOBILITY_IP, $this->_crawl_from ['sina_phone'], TRUE );
				
				if (is_array ( $re_crawl_result ) && ! array_key_exists ( $this->_error_string, $re_crawl_result ) && ! array_key_exists ( $this->_null_data_array_key, $re_crawl_result )) {
					return $this->make_crawl_return_array ( $temp_array, $re_crawl_result );
				} else if (count ( $temp_array ) > 0) {
					return $this->make_crawl_return_array ( $temp_array );
				} else {
					return $re_crawl_result;
				}
			} else {
				return $crawl_use_proxy_ip_result;
			}
		}
	}
	
	/**
	 * 抓取内容验证
	 *
	 * @param
	 *        	$html
	 * @return bool
	 */
	public function match_html($html) {
		if (strpos ( $html, $this->_htmlsearch_config ['no_login'] )) {
			
			// 如果返回登陆页 重新组织参数 更新COOKIE
			$this->make_parameters ();
			
			// $this->label_cookie($this->_cookie['id']);
			return $this->_ci->error_code_library->getError ( 'curl', 'data', 'cookie_error', $this->_htmlsearch_config ['no_login'] );
		} elseif (strpos ( $html, $this->_htmlsearch_config ['result_error'] )) {
			$this->label_proxy_table ( $this->_proxy_ip, $this->_use_proxy );
			return $this->_ci->error_code_library->getError ( 'curl', 'data', 'verification_code', $this->_htmlsearch_config ['result_error'] );
		} elseif (strpos ( $html, $this->_htmlsearch_config ['result_success'] ) === FALSE) {
			return $this->_ci->error_code_library->getError ( 'curl', 'data', 'incomplete_answers', $this->_htmlsearch_config ['result_success'] );
		} else {
			return TRUE;
		}
	}
	private function make_parameters() {
		$post_data = $this->_ci->input->post ();
		if ($post_data) {
			$url = trim ( $post_data ['url'] );
			$pos = strpos ( $url, "?" );
			if (FALSE != $pos) {
				$url = substr ( $url, 0, $pos );
			}
		} else {
			return FALSE;
		}
		
		$url = str_replace ( "\n", "", $url );
		$parameters ['cookie_str'] = true;
		$parameters ['cookie'] = "YF-Page-G0=1ac418838b431e81ff2d99457147068c; SUB=_2AkMhLEVpf8NjqwJRmPodzGznb4x-ywzEiebDAHzsJxIxHkhO7HlZ--_kZTAQVeg10CRAms9G_OfP; SUBP=0033WrSXqPxfM72-Ws9jqgMF55z29P9D9WW4Agg7vFrJvu4.O_vrCUjX; _s_tentry=passport.weibo.com; Apache=7264674508478.492.1450232415008; SINAGLOBAL=7264674508478.492.1450232415008; ULV=1450232415024:1:1:1:7264674508478.492.1450232415008:";
		$parameters ['cookie'] = "SINAGLOBAL=8841271973215.043.1394592901301; uuid_in_cookie=670cc286e235400a9ca1b7c81ec190d2; __utma=15428400.707785078.1423201027.1423201027.1429864086.2; CNZZDATA1257959862=903128963-1459217152-%7C1459217152; CNZZDATA1257836004=1454976703-1459134007-%7C1459301445; wb_publish_vip_2029845227=5; YF-V5-G0=b188043973f8ae1849ba6cd9ae007290; SUHB=0D-EEk1yyP5JVm; _s_tentry=login.sina.com.cn; Apache=7596137163462.084.1460941878190; ULV=1460941878266:382:14:1:7596137163462.084.1460941878190:1460684992379; YF-Page-G0=280e58c5ca896750f16dcc47ceb234ed; YF-Ugrow-G0=1eba44dbebf62c27ae66e16d40e02964; myuid=2029845227; SUB=_2AkMgSButdcPhrAFRmvEdyG7gbopXzQn9vt7yOU7cZ2JCMnoQgT5nqiRotBF_PN72kEe6s1AEYpOgX1pgb98H55OyIzewvB_B3cg.; SUBP=0033WrSXqPxfM72wWs9jqgMF55529P9D9W5FVbJfNnbnz6BlW_Q8cIkq5JpV2hMp1KMRe0.7e2WpMC4odcXt; login_sid_t=29accbbd9bba6f4339e30efbc3021a2c; UOR=sports.sina.com.cn,widget.weibo.com,login.sina.com.cn";
		$parameters ['url'] = $url;
		$this->temp_url = $url;
		$parameters ['method'] = self::METHOD_GET;
		return $this->_parameters = $parameters;
	}
	
	// /由于抓取微博ip被封　但原来的代理ip系统无法使用。故在这里独立为抓取微博详情写了一个使用代理ip的抓取方法。add by 李支锋
	function get_ip_li() {
		$data = $this->send_http_li ( "http://ipapi.unotice.cn/get_ip/main" );
		return $data;
	}
	function send_http_li($url, $timeout = 10, $connecttimeout = 5) {
		$ch = curl_init ();
		curl_setopt ( $ch, CURLOPT_URL, $url );
		curl_setopt ( $ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; rv:19.0) Gecko/20100101 Firefox/19.0' );
		curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, true );
		curl_setopt ( $ch, CURLOPT_FOLLOWLOCATION, true );
		curl_setopt ( $ch, CURLOPT_CONNECTTIMEOUT, $connecttimeout );
		curl_setopt ( $ch, CURLOPT_TIMEOUT, $timeout );
		$rs = curl_exec ( $ch );
		curl_close ( $ch );
		return $rs;
	}
	function get_content_file_li($url) {
		$ch = curl_init ($url);
		curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
		$ip = $this->get_ip_li ();
		if ($ip) {
			curl_setopt ( $ch, CURLOPT_PROXY, $ip );
			curl_setopt ( $ch, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5 );
			curl_setopt ( $ch, CURLOPT_PROXYUSERPWD, 'hongmai:soften.cn' );
		}
		curl_setopt ( $ch, CURLOPT_TIMEOUT, 30 );
		curl_setopt ( $ch, CURLOPT_CONNECTTIMEOUT, 5 );
		curl_setopt ( $ch, CURLOPT_ENCODING, "gzip" );
		curl_setopt ( $ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; rv:19.0) Gecko/20100101 Firefox/19.0' );
		curl_setopt ( $ch, CURLOPT_FOLLOWLOCATION, 1 );
		curl_setopt ( $ch, CURLOPT_COOKIE, $this->_parameters ['cookie'] );
		$content = curl_exec ( $ch );
		return $content;
	}
}
